﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using GiaoDien;
using System.Collections.Generic;

namespace GiaoDien
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["GioHang"] == null)
            {
                Response.Redirect("ListSanPham.aspx");
                return;
            }
            else
            {
                List<ServiceKD.SanPham> lst = (List<ServiceKD.SanPham>) Session["GioHang"];
                GrGioHang.DataSource = lst;
                GrGioHang.DataBind();

                double s = 0;
                for (int i = 0; i < lst.Count; i++)
                {
                    s += (Double)(lst[i].soLuong * lst[i].giaBanQuiDinh);
                }
                lblTongTien.Text = s+"" ;
            }
            if(IsPostBack==false)
            {
                object result = WSProxy.CallWebService("http://moving02.co.cc/ws/Service.asmx", "Service", "LayDanhSachThanhPho", new object[] { });
                DataTable dttp = (DataTable)result;
                cboThanhPhoDen.DataSource = dttp;
                cboThanhPhoDen.DataTextField = dttp.Columns["TenThanhPho"].ToString();
                cboThanhPhoDen.DataValueField = dttp.Columns["MaThanhPho"].ToString();
                cboThanhPhoDen.DataBind();
                cboThanhPhoDen.SelectedIndex = 0;

                object result1 = WSProxy.CallWebService("http://moving02.co.cc/ws/Service.asmx", "Service", "LayHinhThucVanChuyen", new object[] { });
                DataTable dthtvc = (DataTable)result1;
                cboHinhThucVC.DataSource = dthtvc;
                cboHinhThucVC.DataTextField = dthtvc.Columns["TenHinhThuc"].ToString();
                cboHinhThucVC.DataValueField = dthtvc.Columns["MaHinhThuc"].ToString();
                cboHinhThucVC.DataBind();
                cboHinhThucVC.SelectedIndex = 0;
            }
            int HTVC = Int32.Parse(cboHinhThucVC.SelectedValue.ToString());
            int MaTPDen = Int32.Parse(cboThanhPhoDen.SelectedValue.ToString());
            //Mac dinh la Bac Giang va trong luon la 10
            int MaTPGui = 3;
            int TrongLuong = 10;
            object result2 = WSProxy.CallWebService("http://moving02.co.cc/ws/Service.asmx", "Service", "TinhChiPhiVanChuyen", new object[] { MaTPGui, MaTPDen, HTVC, TrongLuong });

            Double kq = (Double)result2;
            lblPhiVanChuyen.Text = kq + "";
            
        }

        protected void btnkiemtrathe_Click(object sender, EventArgs e)
        {
            object result1 = WSProxy.CallWebService("http://moigioimoney04.somee.com/WS/WS4EM.asmx", "WS4EM", "WSLogin", new object[] { "EM11", "123456" });
            txtMaThe.Text = result1.ToString();
        }

        protected void btnXacNhan_Click(object sender, EventArgs e)
        {
            object result = WSProxy.CallWebService("http://moigioimoney04.somee.com/WS/WS4EM.asmx", "WS4EM", "WSLogin", new object[] { "EM11", "123456" });
            String SID = (String)result;
            String CustomerCreditCard = txtMaThe.Text;
            Double AmountMoney = Double.Parse(lblTongChiPhi.Text);
            String CustomerCreditCard_SecureNum = txtMaPin.Text;
            object result1 = WSProxy.CallWebService("http://moigioimoney04.somee.com/WS/WS4EM.asmx", "WS4EM", "TranferMoneyFromCustomer", new object[] { SID, CustomerCreditCard, AmountMoney, CustomerCreditCard_SecureNum });
            int kq = (int)result1;
            if (kq == 0)
            {
                ThemPhieuXuat();
                YeuCauVanChuyen();

                lblThongBao.Text = "Thành công";

            }
            if (kq == 1)
                lblThongBao.Text = "Thất bại";
            if (kq == 2)
                lblThongBao.Text = "Không rõ lỗi";
            if (kq == 3)
                lblThongBao.Text = "Card khách hàng không tồn tại";
            if (kq == 4)
                lblThongBao.Text = "Card của kinh doanh không tồn tại";
            if (kq == -2)
                lblThongBao.Text = "Ngân hàng không hỗ trợ(Môi giới không liên kết với ngân hàng này)";
            if (kq == -1)
                lblThongBao.Text = "SID không tồn tại";
            if (kq == -4)
                lblThongBao.Text = "Card nhà kinh doanh hết hạn";
            if (kq == -3)
                lblThongBao.Text = "Tiền khách hàng không đủ";

            
        }

        public void YeuCauVanChuyen()
        {
            ServiceKD.KhachHang kh = (ServiceKD.KhachHang)Session["DangNhap"];
            ServiceKD.ServiceSoapClient proxy = new GiaoDien.ServiceKD.ServiceSoapClient("ServiceSoap");
            ServiceKD.PhieuXuat[] dsPhieuXuat = proxy.layDonHangKhachHang(kh.maKhachHang);
            int max=dsPhieuXuat[0].maPhieuXuat;
            for (int i = 0; i < dsPhieuXuat.Length; i++)
                if (max < dsPhieuXuat[i].maPhieuXuat)
                    max = dsPhieuXuat[i].maPhieuXuat;
            String MaDonHangCty = max+"";
            int MaTaiKhoanCty = 1;
            string ThongTinKhachHangGiao = "abc";
            string ThongTinKhachHangNhan = "cba";
            int TongTrongLuong = 10;
            int MaThanhPhoDi = 3;
            int MaThanhPhoDen = 1;
            String ghichu = "khong biet";
            int MaHinhThuc = 1;
            object result2 = WSProxy.CallWebService("http://moving02.co.cc/ws/Service.asmx", "Service", "YeuCauVanChuyen", new object[] { MaDonHangCty, MaTaiKhoanCty, ThongTinKhachHangGiao, ThongTinKhachHangNhan, TongTrongLuong, MaThanhPhoDi, MaThanhPhoDen, ghichu, MaHinhThuc });

            String kqvc = (String)result2;

            lblKetQuaVanChuyen.Text = kqvc;
        }

        public void ThemPhieuXuat()
        {
            List<ServiceKD.SanPham> lst = (List<ServiceKD.SanPham>)Session["GioHang"];
            ServiceKD.KhachHang kh = (ServiceKD.KhachHang)Session["DangNhap"];
            ServiceKD.PhieuXuat px = new GiaoDien.ServiceKD.PhieuXuat();
            px.maKhachHang = kh.maKhachHang;
            px.ngayDatMuaHang = DateTime.Now;
            px.tenNguoiNhan = txtNguoiNhan.Text;
            px.diaChiNguoiNhan = txtDiaChi.Text;
            px.tienVanChuyen = Double.Parse(lblPhiVanChuyen.Text);
            px.thanhTien = Double.Parse(lblTongChiPhi.Text);
            px.hinhThucVanChuyen = cboHinhThucVC.SelectedItem.Text;
            px.maTinhTrang = 1;

            ServiceKD.ChiTietPhieuXuat[] ctpx = new GiaoDien.ServiceKD.ChiTietPhieuXuat[lst.Count];
            for (int i = 0; i < lst.Count; i++)
            {
                ServiceKD.ChiTietPhieuXuat temp = new GiaoDien.ServiceKD.ChiTietPhieuXuat();
                temp.maSanPham = lst[i].maSanPham;

                temp.soLuong = (int)lst[i].soLuong;
                temp.donGia = Double.Parse(lst[i].giaBanQuiDinh.ToString());
                ctpx[i] = temp;
            }
            ServiceKD.ServiceSoapClient proxy = new GiaoDien.ServiceKD.ServiceSoapClient("ServiceSoap");
            int kqthempx = proxy.themDonHang(px, ctpx);

            if (kqthempx == 1)
            {
                lst = new List<GiaoDien.ServiceKD.SanPham>();
                Session["GioHang"] = lst;
            }
        }

        protected void cboThanhPhoDen_SelectedIndexChanged(object sender, EventArgs e)
        {
            int HTVC = Int32.Parse(cboHinhThucVC.SelectedValue.ToString());
            int MaTPDen = Int32.Parse(cboThanhPhoDen.SelectedValue.ToString());
            //Mac dinh la Bac Giang va trong luon la 10
            int MaTPGui = 3;
            int TrongLuong = 10;
            object result = WSProxy.CallWebService("http://moving02.co.cc/ws/Service.asmx", "Service", "TinhChiPhiVanChuyen", new object[] { MaTPGui, MaTPDen, HTVC, TrongLuong });

            Double kq = (Double)result;
            lblPhiVanChuyen.Text = kq + "";
        }

        protected void cboHinhThucVC_SelectedIndexChanged(object sender, EventArgs e)
        {
            int HTVC = Int32.Parse(cboHinhThucVC.SelectedValue.ToString());
            int MaTPDen = Int32.Parse(cboThanhPhoDen.SelectedValue.ToString());
            //Mac dinh la Bac Giang va trong luon la 10
            int MaTPGui = 3;
            int TrongLuong = 10;
            object result = WSProxy.CallWebService("http://moving02.co.cc/ws/Service.asmx", "Service", "TinhChiPhiVanChuyen", new object[] { MaTPGui, MaTPDen, HTVC, TrongLuong });

            Double kq = (Double)result;
            lblPhiVanChuyen.Text = kq + "";
        }

        protected void lblTongTien_Load(object sender, EventArgs e)
        {
            Double tiengiohang = 0;
            if(lblTongTien.Text!="")
                tiengiohang= Double.Parse(lblTongTien.Text);
            Double tienvc = 0;
            if (lblPhiVanChuyen.Text != "")
                tienvc= Double.Parse(lblPhiVanChuyen.Text);
            Double tongchiphi = tiengiohang+tienvc;
            lblTongChiPhi.Text = tongchiphi+"";
        }

        protected void lblPhiVanChuyen_Load(object sender, EventArgs e)
        {
            Double tiengiohang = 0;
            if (lblTongTien.Text != "")
                tiengiohang = Double.Parse(lblTongTien.Text);
            Double tienvc = 0;
            if (lblPhiVanChuyen.Text != "")
                tienvc = Double.Parse(lblPhiVanChuyen.Text);
            Double tongchiphi = tiengiohang + tienvc;
            lblTongChiPhi.Text = tongchiphi + "";
        }

    }
}
